﻿using Soft.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Soft.Service
{
    /// <summary>
    /// TimedTaskConfig节点
    /// </summary>
    public sealed class TimedTaskConfig : BaseSubElement
    {

        public TimedTaskConfig(XmlElement current) : base(current)
        {
        }


        /// <summary>
        /// 参数
        /// </summary>
        public string Parameter
        {
            get
            {
                return base.CurrentElement.GetAttribute("Parameter");
            }
            set
            {
                base.CurrentElement.SetAttribute("Parameter", value);
            }
        }


        /// <summary>
        /// 开始时间
        /// </summary>
        public TimeSign StartTime
        {
            get
            {
                return TimeSign.TryParse(base.CurrentElement.GetAttribute("StartTime"), true);
            }
            set
            {
                base.CurrentElement.SetAttribute("StartTime", value.ToString());
            }
        }

     
        /// <summary>
        /// 重复模式
        /// </summary>
        public ScheduleRepeatMode RepeatMode
        {
            get
            {
                return base.CurrentElement.GetAttribute("RepeatMode").ConvertTo(ScheduleRepeatMode.Custom, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("RepeatMode", value.ToString());
            }
        }

        
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime BeginTime
        {
            get
            {
                return base.CurrentElement.GetAttribute("BeginTime").ConvertTo(DateTime.MinValue, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("BeginTime", value.ToDateTimeString(false));
            }
        }

       
        /// <summary>
        /// 循环周期
        /// </summary>
        public TimeUnitItem Cycle
        {
            get
            {
                return (TimeUnitItem)base.CurrentElement.GetAttribute("Cycle");
            }
            set
            {
                base.CurrentElement.SetAttribute("Cycle", value.ToString());
            }
        }


        /// <summary>
        /// TimedTask的名称
        /// </summary>
        public string Name
        {
            get
            {
                return (base.CurrentElement.GetAttribute("Name") ?? "").Trim().ToUpper();
            }
            set
            {
                string text = value.Trim().ToUpper();
                if (text.Length > FunctionCode.INSTANCE_MAX_SIZE)
                {
                    text = text.Substring(0, FunctionCode.INSTANCE_MAX_SIZE);
                }
                base.CurrentElement.SetAttribute("Name", text);
            }
        }


        /// <summary>
        /// 是否允许使用
        /// </summary>
        public bool Enabled
        {
            get
            {
                return base.CurrentElement.GetAttribute("Enabled").ConvertTo(false, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("Enabled", value.ToString());
            }
        }


        /// <summary>
        /// 是否延迟
        /// </summary>
        public bool Delayed
        {
            get
            {
                return base.CurrentElement.GetAttribute("Delayed").ConvertTo(false, true);
            }
            set
            {
                base.CurrentElement.SetAttribute("Delayed", value.ToString());
            }
        }


    }
}
